﻿package anshe.widget
{
	import anshe.event.AnsheNetEvent;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	public class AnsheSWFLoader extends AnsheSuperClient
	{
		private const MIN_WIDTH:Number	= 300;	//	下载条最小的宽度
		private const MIN_HEIGHT:Number	= 60;	//	下载条最小的高度	
		
		public var bgLeft:Sprite;			//	左铡底框
		public var bgMiddle:Sprite;			//	中间底框
		public var bgRight:Sprite;			//	右侧底框
		public var txtStatue:TextField;		//	状态栏
		public var mcLoading:MovieClip;		//	进度条
		public var btnClose:SimpleButton;	//	关闭面板按钮（隐藏）
		
		private var baseWidth:Number;	//	基础宽度
		private var baseHeight:Number;	//	基础高度
		private var fileName:String;	//	需要下载的文件名称

		public function AnsheSWFLoader(swfPath:String = "")
		{
			super(AnsheNetEvent.ACS_SWF_LOADER);
			
			fileName = swfPath;
			
			baseWidth = 0;
			baseHeight = 0;
			
			
			this.statue = "";
			this.percent = 0;
		}
		
		//	设置下载进度百分比
		public function set percent(num:Number):void
		{
			if(num > 0)
			{
				if(num < 100)
				{
					this.statue = "Loading data ... " + Math.floor(num * 10)/10 + " %";
				}
				else
				{
					this.statue = "Loading done.";
				}
				this.mcLoading.gotoAndStop(Math.ceil(num));
			}
			else
			{
				this.statue = "Start Loading...";
				this.mcLoading.gotoAndStop(1);
			}
		}
		
		//	设置状态信息提示
		public function set statue(str:String):void
		{
			this.txtStatue.text = str;
		}
		
		/////////////////////////////////////////////////////////////////////////////////
		
		protected override function InitGame():void
		{
			baseWidth = this.stage.stageWidth;
			baseHeight = this.stage.stageHeight;
			
			onReSizeEvent();			
			
			this.btnClose.addEventListener(MouseEvent.CLICK, onHiddenThis);
			this.stage.addEventListener(Event.RESIZE, onReSizeEvent);
		}
		
		private function onHiddenThis(e:MouseEvent):void
		{
			this.visible = false;
		}
		
		private function onReSizeEvent(e:Event = null):void
		{
			this.x = 0;//(this.stage.stageWidth - this.baseWidth)/2;
			this.y = 0;//(this.stage.stageHeight - this.baseHeight)/2;
			
			if(this.stage)
			{
				var value:Number = this.stage.stageWidth;
				
				value = (value < MIN_WIDTH) ? MIN_WIDTH : value;
				
				this.bgMiddle.width = value - this.bgLeft.width - this.bgRight.width;
				// this.mcLoading.width = value - 80;
				// this.mcLoading.x = this.bgLeft.x + 40;
				
				//this.width = this.stage.stageWidth;
			}
			
			this.bgMiddle.x = this.bgLeft.x + this.bgLeft.width;
			this.bgRight.x = this.bgMiddle.x + this.bgMiddle.width;
			this.btnClose.x = this.bgRight.x;
		}
	}
}